package com.scalar.chiptrack.adaptor.tsmc;

import Acme.HtmlObserver;

import java.net.URL;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

/**
 * Created by ChandraSekhar.
 * Date: June 06, 2005
 * Time: 12:24:22 PM
 */
public class TSMCHtmlObserver implements HtmlObserver
{
    private final static String BV_SESSION_ID = "BV_SessionID";
    private final static String BV_ENGINE_ID = "BV_EngineID";
    private final static String ON_USER_ID = "ON_USER_ID";

    private String m_mainScreenURI = null;
    private String m_productAndMaskInfoURI = null;
    private String m_productInfoURI = null;
    private String m_downloadTextURL = null;
    private String m_downloadCsvURL = null;
    private String m_bvEngineId = null;
    private String m_bvSessionId = null;
    private String m_onUser_ID = null;

    public String getMainScreenURI()
    {
        return m_mainScreenURI;
    }

    public String getProductAndMaskInfoURI()
    {
        return m_productAndMaskInfoURI;
    }

    public String getProductInfoURI()
    {
        return m_productInfoURI;
    }

    public String getDownloadTextURL()
    {
        return m_downloadTextURL;
    }

    public String getDownloadCsvURL()
    {
        return m_downloadCsvURL;
    }

    //Added By ChandraSekhar on 19 Oct 2004
    public void setDownloadCsvURL( String url )
    {

        m_downloadCsvURL = null;
    }

    public String getBvSessionId()
    {
        return m_bvSessionId;
    }

    public void setBvSessionId( String bvSessionId )
    {
        m_bvSessionId = bvSessionId;
    }

    public String getBvEngineId()
    {
        return m_bvEngineId;
    }

    public void setBvEngineId( String bvEngineId )
    {
        m_bvEngineId = bvEngineId;
    }

    public String getOnUserId()
    {
        return m_onUser_ID;
    }

    public void setOnUserId( String onUser_ID )
    {
        m_onUser_ID = onUser_ID;
    }

    public void gotAHREF( String urlString, URL contextUrl, Object clientData )
    {

        if ( urlString == null ) return;

        if ( urlString.indexOf( "pmCatOID=-9904" ) != -1 )
        {
            m_productAndMaskInfoURI = urlString.trim();
        }
        else if ( urlString.indexOf( "pmCatOID=-11173" ) != -1 )
        {
            m_productInfoURI = urlString.trim();
        }

        if ( urlString.indexOf( "download_check" ) != -1 )
        {

            if ( urlString.indexOf( ".csv" ) != -1 )
            {
                m_downloadCsvURL = urlString.trim();
            }

            else if ( urlString.indexOf( ".inf" ) != -1 )
            {
                m_downloadTextURL = urlString.trim();
            }
        }

        if ( urlString.indexOf( "DownloadLog" ) != -1 )
        {

            if ( urlString.indexOf( ".csv" ) != -1 )
            {


                m_downloadCsvURL = urlString.trim();
                m_downloadCsvURL = "http://ectonline.tsmc.com" + m_downloadCsvURL;
            }

            else if ( urlString.indexOf( ".inf" ) != -1 )
            {
                m_downloadTextURL = urlString.trim();
                m_downloadTextURL = "http://ectonline.tsmc.com" + m_downloadTextURL;
            }
        }
    }


    public void gotAREAHREF( String urlString, URL contextUrl, Object clientData )
    {
    }

    public void gotBASEHREF( String urlString, URL contextUrl, Object clientData )
    {
    }

    public void gotBODYBACKGROUND( String urlString, URL contextUrl, Object clientData )
    {
        //System.out.println("urlString = "+urlString);
    }

    public void gotFRAMESRC( String urlString, URL contextUrl, Object clientData )
    {

        //if ((urlString != null) && (urlString.startsWith ("/online/jsp/ECx/on_phome_display.jsp?"))) {
        if ( ( urlString != null ) && ( urlString.startsWith( "/j2ee/TopPage.do" ) ) )
        {

            m_mainScreenURI = urlString;
            System.out.println( "Main URL = " + m_mainScreenURI );
            StringTokenizer st = new StringTokenizer( m_mainScreenURI, "?=&" );
            String token = null;

            while ( st.hasMoreElements() )
            {
                try
                {
                    token = st.nextToken();
                    if ( BV_SESSION_ID.equalsIgnoreCase( token.trim() ) == true )
                    {
                        m_bvSessionId = st.nextToken().trim();
                    }
                    else if ( BV_ENGINE_ID.equalsIgnoreCase( token.trim() ) == true )
                    {
                        m_bvEngineId = st.nextToken().trim();
                    }
                }
                catch ( NoSuchElementException nsee )
                {
                    System.out.println( "***** ERROR *** ERROR *** ERROR ******" );
                    System.out.println( "Exception while retrieving BV_Engine_ID and BV_Session_ID: " + nsee );
                }
            }
        }
    }

    public void gotIMGSRC( String urlString, URL contextUrl, Object clientData )
    {
    }

    public void gotLINKHREF( String urlString, URL contextUrl, Object clientData )
    {
    }

}
